﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Database;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    public partial class CopyDbEditor : Form, IComponentEditor
    {
        private object _ActivityNode;
        private CopyDbComponent _CopyDb;

        /// <summary>
        /// 
        /// </summary>
        public CopyDbEditor()
        {
            InitializeComponent();
        }

        private void CopyDbEditor_Load(object sender, EventArgs e)
        {
            this.cbo_SrcProviders.Items.Clear();
            this.cbo_TgtProviders.Items.Clear();
            foreach(DataProviderType dbProvider in Enum.GetValues(typeof(DataProviderType)))
            {
                this.cbo_TgtProviders.Items.Add(dbProvider.ToString());
                this.cbo_SrcProviders.Items.Add(dbProvider.ToString());
            }
        }

        #region Implementation of IComponentEditor

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get
            {
                return _ActivityNode;
            }
            set
            {
                _ActivityNode = value;
            }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get
            {
                return typeof(CopyDbComponent);
            }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents, object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._CopyDb = (CopyDbComponent)wfComponent;

            CopyDbInstruction instruction =
                (CopyDbInstruction)this._CopyDb.Instruction;
            this.cbo_SrcProviders.Text = instruction.SrcDbProvider.ToString();
            this.txt_SrcConn.Text = instruction.SrcConnStr;
            this.PopulateTables();
            if(this.clb_Tables.Items.Count>0 && instruction.TablesToCopy !=null && 
                instruction.TablesToCopy.Count>0)
            {
                List<string> tblsToCopy =new List<string>();
                foreach(string schemaName in instruction.TablesToCopy.Keys)
                {
                    foreach(string tblName in instruction.TablesToCopy[schemaName])
                    {
                        tblsToCopy.Add(schemaName + "." + tblName);
                    }
                }

                for(int i=0;i<this.clb_Tables.Items.Count;i++)
                {
                    if(tblsToCopy.Contains(this.clb_Tables.Items[i].ToString()))
                        this.clb_Tables.SetItemChecked(i,true);
                    else 
                        this.clb_Tables.SetItemChecked(i,false);
                }
            }

            this.cbo_TgtProviders.Text = instruction.TgtDbProvider.ToString();
            this.txt_TgtConn.Text = instruction.TgtConnStr;

        }

        /// <summary></summary>
        public void SaveSettings()
        {
            CopyDbInstruction instruction =
               (CopyDbInstruction)this._CopyDb.Instruction;

            instruction.SrcConnStr = this.txt_SrcConn.Text;
            instruction.TgtConnStr = this.txt_TgtConn.Text;
            instruction.SrcDbProvider = (DataProviderType) Enum.Parse(
                                                               typeof (DataProviderType),
                                                               this.cbo_SrcProviders.Text);
            instruction.TgtDbProvider =
                (DataProviderType) Enum.Parse(typeof (DataProviderType), this.cbo_TgtProviders.Text);
            instruction.TablesToCopy = new Dictionary<string, List<string>>();
            for(int i=0;i<this.clb_Tables.Items.Count;i++)
            {
                if(this.clb_Tables.GetItemChecked(i))
                {
                    string[] parts = this.clb_Tables.Items[i].ToString().Split(new char[] {'.'});
                    string schemaName = parts[0];
                    string tblName = parts[1];
                    if(instruction.TablesToCopy.ContainsKey(schemaName))
                    {
                        List<string> tblNames = instruction.TablesToCopy[schemaName];
                        tblNames.Add(tblName);
                        instruction.TablesToCopy[schemaName] = tblNames;
                    }
                    else
                    {
                        List<string> tblNames=new List<string>();
                        tblNames.Add(tblName);
                        instruction.TablesToCopy.Add(schemaName, tblNames);
                    }
                }
            }

            this._CopyDb.Instruction = instruction;
        }

        /// <summary></summary>
        public TestResult Test()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        private void cbo_GetTables_Click(object sender, EventArgs e)
        {
            this.PopulateTables();
        }

        private void PopulateTables()
        {
            if(!string.IsNullOrEmpty(this.txt_SrcConn.Text))
            {
                string connStr = this.txt_SrcConn.Text;
                DataProviderType dbProvider = DataProviderType.MSSQL;
                if(this.cbo_SrcProviders.SelectedIndex>=0)
                {
                    dbProvider = (DataProviderType) Enum.Parse(typeof (DataProviderType), this.cbo_SrcProviders.Text);
                }
                this.clb_Tables.Items.Clear();
                Dictionary<string, List<string>> schemaTbls
                    = DbUtil.GetTableNamesWithSchemas(dbProvider, connStr);
                if (schemaTbls != null && schemaTbls.Count > 0)
                {
                    foreach (string schemaName in schemaTbls.Keys)
                    {
                        foreach (string tblName in schemaTbls[schemaName])
                        {
                            this.clb_Tables.Items.Add(schemaName + "." + tblName);
                        }
                    }
                }
            }
            
        }

        private void btn_SelectAllTables_Click(object sender, EventArgs e)
        {
            if(this.clb_Tables.Items.Count>0)
            {
                for(int i=0;i<this.clb_Tables.Items.Count;i++)
                {
                    this.clb_Tables.SetItemChecked(i, (!this.clb_Tables.GetItemChecked(i)));
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._ActivityNode, this._CopyDb);
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
